import { $message,$api } from "@/utils";
import { useWebConfigStore, useUserStore, useRefs } from "@/stores";
import * as $helper from '@/utils/helper';
const XUndefined = void (0);
type XUndefined = typeof XUndefined;
const buildOptions = (options: UniNamespace.NavigateToOptions | UniNamespace.RedirectToOptions | UniNamespace.ReLaunchOptions | UniNamespace.SwitchTabOptions, login?: boolean) => {
	// #ifdef H5
	const appid = $helper.appid();
	if (options.url.indexOf('?') > -1) {
		options.url += '&';
	} else {
		options.url += '?';
	}
	options.url += `appid=${appid}`;
	// #endif
	const UserStore = useUserStore();
	if (login) {
		if (!UserStore.hasLogin()) {
			options.url = '/pages/login/login';
			uni.navigateTo(options);
			return XUndefined;
		}
		if (options.url.startsWith('/technician/pages/') && !options.url.includes('/technician/pages/user/apply')) {
			if (!UserStore.hasPermission('technician')) {
				options.url = '/technician/pages/user/apply';
				uni.navigateTo(options);
				return XUndefined;
			}
		}
		if (options.url.startsWith('/agent/pages/') && !options.url.includes('/agent/pages/user/apply')) {
			if (!UserStore.hasPermission('agent')) {
				options.url = '/agent/pages/user/apply';
				uni.navigateTo(options);
				return XUndefined;
			}
		}
		if (options.url.startsWith('/channels/pages/') && !options.url.includes('/channels/pages/user/apply')) {
			if (!UserStore.hasPermission('channels')) {
				options.url = '/channels/pages/user/apply';
				uni.navigateTo(options);
				return XUndefined;
			}
		}
	}
	return options;
}
export const $page = {
	open: (params: UniNamespace.NavigateToOptions | string, hasLogin?: boolean) => {
		let options: UniNamespace.NavigateToOptions | XUndefined = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		options = buildOptions(options, hasLogin);
		if (options) {
			uni.navigateTo(options);
		}
	},
	redirect: (params: UniNamespace.RedirectToOptions | string, hasLogin?: boolean) => {
		let options:UniNamespace.RedirectToOptions | XUndefined = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		options = buildOptions(options, hasLogin);
		if (options) {
			uni.redirectTo(options)
		}
	},
	re: (params: UniNamespace.ReLaunchOptions | string, hasLogin?: boolean) => {
		let options:UniNamespace.ReLaunchOptions|XUndefined = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		options = buildOptions(options, hasLogin);
		if (options) {
			uni.reLaunch(options)
		}
	},
	switch: (params: UniNamespace.SwitchTabOptions | string) => {
		let options:UniNamespace.SwitchTabOptions|XUndefined = {
			url: ''
		};
		if (typeof params === 'string') {
			options.url = params;
		} else {
			options = Object.assign(options, params);
		}
		options = buildOptions(options);
		if (options) {
			uni.switchTab(options);
		}
	},
	back: (delta?: number) => {
		if (getCurrentPages().length < 2) {
			return uni.reLaunch({
				url: '/pages/index/index'
			});
		}
		uni.navigateBack({
			delta: delta || 1
		})
	},
	ads: (item: AdsInterface) => {
		switch (item.page) {
			case "INNER":
				$page.open(item.url);
				break;
			case "ARTICLE":
				$page.open(`/pages/article/content?id=${item.alias_id}`);
				break;
			case "SWITCH":
				$page.switch(item.url);
				break;
			case "CUSTOM":
				// #ifdef H5
				globalThis.open(item.url);
				// #endif
				break;
		}
	},
	title: (title?: string, subtitle?: string) => {
		const WebConfigStore = useWebConfigStore();
		const { WEBCONFIG } = useRefs(WebConfigStore);
		const fastTitle = title || WEBCONFIG.value?.web_name || $helper.name();
		const lastTitle = subtitle !== undefined ? subtitle : '';
		uni.setNavigationBarTitle({
			title: fastTitle + lastTitle
		})
	},
	shareMessage(options:any){
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const userStore=useUserStore();
		let obj:any={
			...options
		};
		obj.title=WEBCONFIG.value?.share_message_title||options.title||$helper.name();
		obj.imageUrl=WEBCONFIG.value?.share_message_image||options.imageUrl||'';
		if(userStore.hasLogin()){
			const {USERINFO}=useRefs(userStore);
			if(obj.path.indexOf('?')>-1){
				obj.path+='&';
			}else{
				obj.path+='?';
			}
			obj.path+=`puid=${USERINFO.value.id}`;
		}
		return obj;
	},
	shareTimeline(options:any){
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const userStore=useUserStore();
		let obj:any={
			...options
		};
		obj.title=WEBCONFIG.value?.share_timeline_title||options.title||$helper.name();
		obj.imageUrl=WEBCONFIG.value?.share_timeline_image||options.imageUrl||'';
		if(userStore.hasLogin()){
			const {USERINFO}=useRefs(userStore);
			if(obj.path.indexOf('?')>-1){
				obj.path+='&';
			}else{
				obj.path+='?';
			}
			obj.path+=`puid=${USERINFO.value.id}`;
		}
		return obj;
	},
}
export const login = (data: any, xLoginRef: any) => {
	xLoginRef.show(data);
}
export const wxLogin = (data: any) => {
	if (!$helper.isWechat()) {
		return $message.error('请先安装微信客户端');
	}
	const WebConfigStore = useWebConfigStore();
	const { WEBCONFIG } = useRefs(WebConfigStore);
	if (WEBCONFIG.value?.wx_state) {
		$api.Login.wechatLogin(data)
			.then((res:any) => {
				globalThis.location.href = res.data;
			}).catch((err:any) => {
				$message.error(err.msg);
			})
	} else {
		$message.error('请先配置微信公众号');
	}
}